﻿using System;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BDElectron.Remote;
using BDElectron.ClientPanels;


namespace BDElectron.Client
{
    public partial class MainForm : Form
    {
        IServerObject mServerObject = null;

        public MainForm(IServerObject serverObject)
        {
            InitializeComponent();
            mServerObject = serverObject;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.Text += " <<You loget as '" + mServerObject.User + "'>>";
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();    
        }

        private TabPage AddUserControlInTabs(UserControl userControl)
        {
            TabPage tabPage = null;
            // If not exist
            if (!mainTabControl.TabPages.ContainsKey(userControl.Name))
            {
                // Add it
                mainTabControl.TabPages.Add(userControl.Name, userControl.Name);

                tabPage = mainTabControl.TabPages[mainTabControl.TabPages.IndexOfKey(userControl.Name)];

                tabPage.ImageIndex = 2;
                tabPage.Controls.Add(userControl);
                userControl.Dock = DockStyle.Fill;
                mainTabControl.SelectedTab = tabPage;
                tabPage.Focus();
            }
            else
            {
                // Make it active
                tabPage = mainTabControl.TabPages[mainTabControl.TabPages.IndexOfKey(userControl.Name)];
                mainTabControl.SelectedTab = tabPage;
                tabPage.Focus();
            }


            Update();

            return tabPage;
        }


        private void maintainUsersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MaintainUsersPanel administrationPanel = new MaintainUsersPanel(mServerObject);
            administrationPanel.Name = "Maintain users";
            AddUserControlInTabs((UserControl)administrationPanel);
        }
    }
}
